package controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.FullJobState;
import beans.HttpConfig;
import beans.HttpResult;
import beans.HttpResultList;
import beans.RunningTask;
import dao.JobDao;
import engine.BIEngine0_1.AlgorithmOperator;
import engine.BIEngine0_1.AlgorithmThread;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GetFullState
 */
public class GetFullState extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetFullState() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String taskHash = request.getParameter("taskHash");
			String blockId = request.getParameter("blockId");
			String jobId = request.getParameter("jobId");
			String operate = request.getParameter("operate");
			JobDao jonDao = new JobDao();
			List<Object> results;
			if (jobId == null && operate == null) {
				results = jonDao.getFullAlgorithmState(taskHash, blockId);
				response.getWriter().println(results.get(0));
			} else if (operate == null) {
				results = jonDao.getFullJobState(taskHash, blockId, jobId);
				JSONArray resultsJSONArray = JSONArray.fromObject(results.get(0));
				JSONObject resultsJSONObject = JSONObject.fromObject(resultsJSONArray.get(0));
				if (resultsJSONObject.get("fullInfo") == null) {
					HttpResultList result = null;
					HttpResult tokenResult = AlgorithmOperator.getToken(HttpConfig.userName, HttpConfig.passWord);
					if (tokenResult.isResult()) {
						try {
							result = AlgorithmOperator.getJobFullState(jobId, tokenResult.getMessage());
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					FullJobState fullJobState = (FullJobState) result.getDatas().get(0);
					JSONObject jsonObject = JSONObject.fromObject(fullJobState);
					resultsJSONObject.put("fullInfo", jsonObject);

					resultsJSONArray.remove(0);
					resultsJSONArray.add(resultsJSONObject);

					results.remove(0);
					results.add(resultsJSONArray);
				}
				response.getWriter().println(results.get(0));
			} else if (operate.equals("refresh")) {
				JSONObject result = new JSONObject();
				JobDao jobDao = new JobDao();
			/*	if(jobDao.getJobState(taskHash,blockId,jobId)){
					//作业已经结束，不予刷新
					result.put("result", "end");
				}else {
					//作业未结束，刷新
					AlgorithmThread algorithmThread = RunningTask.TASKMAP.getTask(taskHash).runningTaskBean.runningAlgorithms.get(blockId);
					boolean bool = algorithmThread.refreshJobFullState(taskHash,blockId,jobId);
					if (bool){
						result.put("result", "true");
					}else {
						result.put("result", "false");
					}
				}*/
				boolean b =updateJobState(taskHash,blockId,jobId);
				if(b){
					result.put("result", "true");
				}else{
					result.put("result", "false");
				}
				response.getWriter().println(result.toString());
			} else {
				results = jonDao.getFullJobState(taskHash, blockId, null);
				response.getWriter().println(results.get(0));
			}
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace();
			response.getWriter().println("error");
		}
	}
	
	private boolean updateJobState(String taskHash,String blockId, String jobId){
		
		return AlgorithmThread.refreshJobFullState2(taskHash, blockId, jobId);
	}
}